
<style>
    .center{
        text-align:center;
    }
    .table-action-area{
        float: right;
    }
</style>
<div class="layui-fluid layui-anim website-anim" id="website-standard-add" lay-title="评审标准管理新增">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full" id="standard-add">
                    <form class="layui-form layui-table-form" lay-filter="standard-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md12">
                                <div class="layui-form-item">
                                    <!--隐藏数据-->
                                    <input type="hidden" id="hiddenUdateDate">
                                    <div class="layui-inline layui-col-md6">
                                        <label class="layui-form-label website-form-item-require">名称:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="name" autocomplete="off" lay-verify="required" class="layui-input" >
                                        </div>
                                    </div>
                                    <div class="layui-inline layui-col-md6">
                                        <label class="layui-form-label website-form-item-require">要求说明:</label>
                                        <div class="layui-input-block">
                                            <textarea name="explainContent" placeholder="要求说明" rows="3"  lay-verify="required" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="table-action-area">
                            <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" id="reviewDetailAdd">
                                新增列表
                            </div>
                        </div>
                        <div class="layui-row">
                            <table lay-filter="standardTableAdd" lay-data="{id: 'standardTableAdd'}"></table>
                        </div>
                        <span>&nbsp;</span>
                        <span>&nbsp;</span>
                        <div class="layui-form-item center">
                            <button class="layui-btn" lay-submit="" lay-filter="standard-add-form-submit" id="submit">保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="standard-detail-option">
    <a lay-event="edit" shiro:hasPermission="standard:update" class="website-blue">修改</a>
    <a lay-event="del" shiro:hasPermission="standard:delete" class="website-red">删除</a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
             $view = $('#website-standard-add'),
            $reviewDetailAdd = $view.find('#reviewDetailAdd'),
            $searchForm = $view.find('form'),
            tableIns,
            createTimeFrom,
            createTimeTo,
            validate = layui.validate;
        form.verify(validate);
        initTable();
        form.render();
        form.on('submit(standard-add-form-submit)', function (data) {
              var dateSoure=data.field;
              var cacheDataDel = table.cache["standardTableAdd"];
              if(cacheDataDel.length==0){
                  website.alert.error('必须新增评审标准列表');
                  return false;
              }
              for (var key  in cacheDataDel){
                  delete cacheDataDel[key].LAY_TABLE_INDEX;
                  delete cacheDataDel[key].indicatorType;
                  delete cacheDataDel[key].maxScore;
                  delete cacheDataDel[key].minScore;
                  delete cacheDataDel[key].questionDescription;
              }
              dateSoure.listSoure=cacheDataDel;
              website.post(ctx + 'standard',{'json':JSON.stringify(dateSoure)}, function () {
                  layer.closeAll();
                  //跳转到第一页面
                  var currentUrl = window.location.hash.replace('#', '');
                  website.view.tab.del(currentUrl);
                  window.location.reload();
              });
            return false;
        });
        table.on('tool(standardTableAdd)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'del') {
                website.modal.confirm('删除信息', '确定删除该信息？', function () {
                    var cacheDataDel = table.cache["standardTableAdd"];
                        cacheDataDel.splice(obj.tr.data('index'), 1)//根据索引删除当前行
                        tableIns.reload({
                            data: cacheDataDel
                        });
                });
            }
            if (layEvent === 'edit') {
                var data = obj.data
                delete data.indicatorType;
                delete data.maxScore;
                delete data.minScore;
                delete data.questionDescription;

                var indexNum=obj.tr.data('index');
                var strJson = JSON.stringify(data);
                $("#hiddenUdateDate").val(strJson)
                website.modal.open('修改专家评审结果详细', 'biz/standard/detail/update', {
                    btn: ['修改'],
                    offset: '30px',
                    scrollbar: true,
                    yes: function (index, layero) {
                        $('#standard-detail-update').find('#submit').trigger('click');
                        //先删除原数据
                        var cacheDataDel = table.cache["standardTableAdd"];
                        cacheDataDel.splice(obj.tr.data('index'), 1)//根据索引删除当前行
                        tableIns.reload({
                            data: cacheDataDel
                        });
                        //插入新数据
                        var updateSoure = $('#standard-detail-update').find('#hiddenUpdateDate').val();
                        debugger
                        var objJson = JSON.parse(updateSoure);
                        var cacheData = table.cache["standardTableAdd"];
                        cacheData.push(objJson);
                        tableIns.reload({
                            data : cacheData
                        });
                        layer.closeAll();
                        website.alert.success('修改成功');
                    }
                });
            }
        });
        table.on('sort(standardTableAdd)', function (obj) {
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'standardTableAdd'
                ,limit: 10 //注意：请务必确保 limit 参数（默认：10）是与你服务端限定的数据条数一致
                ,cols: [[
                    {type: 'numbers', title: '序号'},
                    {field: 'name', title: '名称', minWidth: 100},
                    {field: 'description', title: '描述', minWidth: 100},
                    {field: 'weight', title: '权重', minWidth: 100},
                    {title: '操作',align:'center', toolbar: '#standard-detail-option', minWidth: 200}
                ]],
                data: []
            });
        }
        $reviewDetailAdd.on('click', function () {
            website.modal.open('新增专家评审结果详细', 'biz/standard/detail/add', {
                btn: ['提交'],
                offset: '30px',
                scrollbar: true,
                yes: function (index, layero) {

                    $('#standard-detail-add').find('#submit').trigger('click');
                    var da = $('#standard-detail-add').find('#hiddenDate').val();
                    var objJson = JSON.parse(da);
                    var cacheData = table.cache["standardTableAdd"];
                    cacheData.push(objJson);
                    tableIns.reload({
                        data : cacheData
                    });
                    layer.closeAll();
                    website.alert.success('新增成功');
                }
            });
        });

        function getQueryParams() {
            var params = $searchForm.serializeJson();
            var createTime = params.time;
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            params.invalidate_ie_cache = new Date();
            params.createTimeFrom = createTimeFrom;
            params.createTimeTo = createTimeTo;
            return params;
        }

    })
</script>